<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
class SalesController extends AdminController{
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}
	public function index(){
		$where["status"]=array("NEQ","-1");
		$count = M('About')->count();// 查询满足要求的总记录数
		$Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list=M('Sales')->where($where)->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display();		
	}
	public function insert(){
		$this->display();
	}
	public function add(){
      if($_FILES["pic"]["name"]){
            $file=$_FILES["pic"];
            $data['pic']=$this->upload($file);
        }
    	if(empty($data['pic'])){
    		$this->error("请上传图片");
    	}
    	$data["content"]=htmlspecialchars_decode($_POST["content"]);	
		$data['status']=I('status');
		$data['add_time']=time();
		$list=M('Sales')->add($data);
		if($list){
			$this->success('添加成功',U('Sales/index'));
		}else{
			$this->error('添加失败');
		}
	}
	public function edit(){
		$where['id']=I('id');
		$list=M('Sales')->where($where)->select();
		$this->assign('list',$list);
		$this->display(); 
	}
	public function update(){
		$where['id']=I('id');
		//删除原文件
      if($_FILES["pic"]["name"]){
            $file=$_FILES["pic"];
            $url=M('Sales')->where($where)->find();
            unlink($_SERVER['DOCUMENT_ROOT'].$url['pic']);
            $data['pic']=$this->upload($file);
        }
    	$data["content"]=htmlspecialchars_decode($_POST["content"]);        
		$data['status']=I('status');
		$data['add_time']=time();
		$list=M('Sales')->where($where)->save($data);
		if($list){
			$this->success('修改成功',U('Sales/index'));
		}else{
			$this->error('您未作任何修改');
		}
	}
		public function open(){
    	$where['id']=I('id')?I('id'):null;
    	$list=M('Sales')->where($where)->find();
    	if($list['status']==0){
    		$list=M('Sales')->where($where)->setField('status','1');
    		$this->success('状态修改成功');
    	}
    	if($list['status']==1){
    		$list=M('Sales')->where($where)->setField('status','0');
    		$this->success('状态修改成功');
    	}
    }
	public function delete(){
    	$where['id']=I("id","");
		if(!empty($where['id'])){
			$list=M('Sales')->where($where)->find();
			if($list["status"]==0){
				$res=M('Sales')->where($where)->setField('status','-1');
				if($res){
					$this->success('删除成功',U('Sales/index'));
				}else{
					$this->error('删除失败');
				}
			}else{
				$this->error("显示状态下不能删除");
			}
		} 
    }




}